home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Pascal
/
Book Demos in Pascal
/
CopyBits & CopyMask
/
CopyBits meets CopyMask.p
< prev
next >
Wrap
Text File
|
1995-04-04
|
4KB
|
151 lines
program CopyBitsMeetsCopyMask;
uses
{$IFC UNDEFINED THINK_PASCAL}
Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
OSUtils, ToolUtils, OSEvents,
{$ENDC}
QDOffScreen;
(* Standard inits *)
const
kLoopNumber = 100;
procedure InitToolbox;
begin
{$IFC UNDEFINED THINK_PASCAL}
InitGraf(@qd.thePort);
InitFonts;
FlushEvents(everyEvent, 0);
InitWindows;
InitMenus;
TEInit;
InitDialogs(nil);
{$ENDC}
InitCursor;
end; (*InitToolbox*)
var
myWindow: WindowPtr;
windowRectangle: Rect;
h, v: Integer;
screenPort: GrafPtr;
screenDevice: GDHandle;
pictRectangle, srcRectangle: Rect;
coolPicture, maskPicture: PicHandle;
cTable: CTabHandle;
offscreenGWorld, maskGWorld: GrafPtr;
maskRegion: RgnHandle;
{ offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to}
{ refer to them by GrafPtrs, since the CopyBits calls get much cleaner.}
(* Variables for loops, timing and display *)
beforeTime, afterTime: LongInt;
loop: LongInt;
tempStr: Str255;
begin
InitToolbox;
(* Load the picture*)
coolPicture := GetPicture(128); (*PICT resource #128.*)
if (coolPicture = nil) then
begin
SysBeep(1);
ExitToShell;
end;
maskPicture := GetPicture(129); (*PICT resource #129.*)
if (maskPicture = nil) then
begin
SysBeep(1);
ExitToShell;
end;
(* Get its frame *)
pictRectangle := coolPicture^^.picFrame;
(* Move it to 0,0 *)
OffsetRect(pictRectangle, -pictRectangle.left, -pictRectangle.top);
(*Set up the window*)
windowRectangle := pictRectangle;
OffsetRect(windowRectangle, 64, 64);
windowRectangle.bottom := windowRectangle.bottom + 100;
myWindow := NewCWindow(nil, windowRectangle, 'CopyBitsSpeedTest', true, 0, WindowPtr(-1), false, 0);
SetPort(myWindow);
{ DrawPicture(coolPicture, &pictRectangle);}
(* Remember it so we can get back after visiting a GWorld *)
GetGWorld(GWorldPtr(screenPort), screenDevice);
(* Create a GWorld to draw the PICT in *)
if (noErr <> NewGWorld(GWorldPtr(offscreenGWorld), 0, pictRectangle, nil, nil, [])) then
ExitToShell;
(*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*)
if LockPixels(CGrafPtr(offscreenGWorld)^.portPixMap) then ;
SetGWorld(GWorldPtr(offscreenGWorld), nil);
PaintRect(pictRectangle);
DrawPicture(coolPicture, pictRectangle);
(* Create a GWorld to draw the mask in *)
if (noErr <> NewGWorld(GWorldPtr(maskGWorld), 1, pictRectangle, nil, nil, [])) then
ExitToShell;
(*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*)
if (LockPixels(CGrafPtr(maskGWorld)^.portPixMap)) then ;
SetGWorld(GWorldPtr(maskGWorld), nil);
EraseRect(pictRectangle);
DrawPicture(maskPicture, pictRectangle);
maskRegion := NewRgn;
if noErr <> BitMapToRegion(maskRegion, maskGWorld^.portBits) then
begin
end; {I ignore errors here}
(* GWorlds done. Set back port and device! *)
SetGWorld(GWorldPtr(screenPort), screenDevice);
CopyBits(offscreenGWorld^.portBits, myWindow^.portBits, pictRectangle, pictRectangle, srcCopy, nil);
{ CopyBits}
srcRectangle := pictRectangle;
beforeTime := TickCount;
for loop := 0 to kLoopNumber - 1 do
begin
srcRectangle := pictRectangle;
OffsetRect(srcRectangle, 0, loop mod 20 - 10);
CopyBits(offscreenGWorld^.portBits, myWindow^.portBits, srcRectangle, pictRectangle, srcCopy, maskRegion);
end;
afterTime := TickCount;
MoveTo(10, windowRectangle.bottom - 80 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString('CopyBits: ');
DrawString(tempStr);
DrawString(' ticks.');
beforeTime := TickCount;
for loop := 0 to kLoopNumber - 1 do
begin
srcRectangle := pictRectangle;
OffsetRect(srcRectangle, 0, loop mod 20 - 10);
CopyMask(offscreenGWorld^.portBits, maskGWorld^.portBits, myWindow^.portBits, srcRectangle, pictRectangle, pictRectangle);
end;
afterTime := TickCount;
MoveTo(10, windowRectangle.bottom - 50 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString('CopyMask: ');
DrawString(tempStr);
DrawString(' ticks.');
while not Button do
;
end. (*main*)